#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use gri_macro_handler qw(get_uploaded_file_handle link_to_str);
use gri_database;
use File::Temp qw(tempfile);
use Digest::SHA1;

sub mm_form_docrepos_upload_file_process {
	my $str="";

	my $link;
	if(exists($::PARAM{lp})) {
		my $lp=pack("h*",$::PARAM{lp});
		$link=link_to_str("","",$lp,"");
	} else {
		$link=link_to_str("","","/form_docrepos_upload_file","");

	}
	my $max_upload_size=10; # Maximum MB upload size (default)
	my $rr_max_upload_size=$::GRI_FRONTEND->get_variable("MAX_UPLOAD_SIZE");
	if($rr_max_upload_size !~ /not defined/) {
		$max_upload_size=int($rr_max_upload_size);
		$max_upload_size=10 if $max_upload_size<1;
	}
	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");
	
	my $file_name=$::PARAM{name};
	my $file_description=$::PARAM{description};
	my $file_actual=$::PARAM{actual};
	my $uploader=$::PARAM{uploader};
	my $fh=get_uploaded_file_handle("actual");
	my ($wfh,$tfile)=tempfile(DIR=>$docrepos, SUFFIX=>".gri");
	my $bytesread; my $buffer;
	my $fsize=0;
	my $ctx = Digest::SHA1->new;
	my $failed=0;
	my $toobig=0;
	while ($bytesread=read($fh,$buffer,1024)) {
	   if(! print $wfh $buffer) {
	   	$failed=1;
		last;
	   }
	   $fsize+=$bytesread;
	   if($fsize>($max_upload_size*1024*1024)) {
	   	$toobig=1;
		last;
	   }
	   $ctx->add($buffer);
	}
	close($fh);
	close($wfh);

	# If the write out failed then we abort before processing
	# it ... deleting the necessary temporary files...
	if($failed) {
		unlink($tfile) if -f $tfile;
		my $serror="Error Uploading Document";
		my $error="Ran out of disk space whilst uploading document '$file_actual'";
		$link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$link.=",previouspage=form_docrepos_upload_file::$s";
		redirect_header(path => $link);
		exit;
	}
	if($toobig) {
		unlink($tfile) if -f $tfile;
		my $serror="Error Uploading Document";
		my $error="The file '$file_actual' is too large to be uploaded.";
		$link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$link.=",previouspage=form_docrepos_upload_file::$s";
		redirect_header(path => $link);
		exit;
	}
	my $digest=$ctx->hexdigest;

	# Now we have a copy of the uploaded file and the SHA1
	# digest ... 

	my ($a,$b)=$DB->get_details_by_sha1(sha1 => $digest);

	# Ok the file has already been uploaded, so we simply add
	# to the list of sections it appears in!
	my @S=();
	for my $cparam (keys %::PARAM) {
		if($cparam =~ /^section_(.*)/) {
			push @S,$1;
		}
	}
	my $sec=join(",",@S);

	if($a==1) {
		$a=$DB->add_doc_to_sections(content_sha1=>$digest,local_name=>$b->{local_name},sections=>$sec,description=>$file_description, original_name=>$file_actual,docrepos=>$docrepos,uploader=>$uploader);
		unlink($tfile) if -f $tfile;
		if($a==1) {
			redirect_header(path => $link);
			exit;
		}
		my $serror="Error Uploading Existing Document";
		my $error="Unable to change the list of sections which the existing document '$file_actual' exists in. RC=$a";
		$link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$link.=",previouspage=form_docrepos_upload_file::$s";
		redirect_header(path => $link);
		exit;
	}

	# if a!=0 issue an error ...
	if($a!=0) {
		my $serror="Error Uploading Document";
		my $error="Unable to check the document repository for the document '$file_actual'. RC=$a";
		$link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$link.=",previouspage=form_docrepos_upload_file::$s";
		redirect_header(path => $link);
		exit;
	}

	# Ok the document does not exist, so we need to add the new document to the database ...

	($a,$b)=$DB->add_new_document(content_sha1=>$digest,sections=>$sec,description=>$file_description, original_name=>$file_actual,docrepos=>$docrepos,temporary_file=>$tfile,uploader=>$uploader);
	unlink($tfile) if -f $tfile;
	if($a!=1) {
		my $serror="Error Uploading New Document";
		my $error="Unable to upload the document '$file_actual'. RC=$a";
		$link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		# We need to encode the current data for repopulation into the form!
		my $s="";
		$s.="file_name=".string_to_hex($file_name);
		$s.="::file_description=".string_to_hex($file_description);
		$s.="::file_actual=".string_to_hex($file_actual);
		$link.=",previouspage=form_docrepos_upload_file::$s";
		redirect_header(path => $link);
		exit;
	}
	redirect_header(path => $link);
	exit;
}

